package administrator;

import java.awt.EventQueue;

public class DelQues {

	public JFrame frame;
	private final JLabel lblEnterTheQuestion = new JLabel("Enter the Question Id you want to delete ");
	private JTextField QIDfield;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DelQues window = new DelQues();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public DelQues() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(385, 200, 460, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		lblEnterTheQuestion.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblEnterTheQuestion.setHorizontalAlignment(SwingConstants.CENTER);
		lblEnterTheQuestion.setBounds(10, 33, 424, 38);
		frame.getContentPane().add(lblEnterTheQuestion);
		
		QIDfield = new JTextField();
		QIDfield.setBounds(169, 108, 86, 20);
		frame.getContentPane().add(QIDfield);
		QIDfield.setColumns(10);
		
		JButton btnOk = new JButton("Ok");
		btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });
		btnOk.setBounds(169, 175, 86, 23);
		frame.getContentPane().add(btnOk);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
		btnCancel.setBounds(169, 215, 89, 23);
		frame.getContentPane().add(btnCancel);
	}
	
	public int DelQuestion(String s)
	{
		String str;
		int flag = 0;
		int count = 0;
		
		str= "delete from ques_paper where q_id = '"+s+"'" ;	//for delete
		
		try
        {
            Class.forName("java.sql.Driver");
            
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/oats","root","anupam24");
            
            Statement stmt = con.createStatement();
           
            count = stmt.executeUpdate(str);
             
            System.out.println(count);
            if(count == 0)
            {
            	flag = 2;
            }
            else
            {
            	flag = 1;
            }
            
            stmt.close();
            con.close();
            
            return flag;
        }
        
		catch(Exception e)
        {
            System.out.println("Error is : "+e.getMessage());
            flag = 0;
            return flag;
        }
        
	}
	
	private void btnOkActionPerformed(java.awt.event.ActionEvent evt)
	{
		String s = QIDfield.getText();
		int flag = -1;
		
		if(s.length() == 0)
		{
			JOptionPane.showMessageDialog(null,"Please Enter QID" ,"ERROR", JOptionPane.ERROR_MESSAGE);
		}
		else
		{
			flag = DelQuestion(s);
		}
		
		
		if(flag == 0)
		{
			JOptionPane.showMessageDialog(null,"Some internal error occured" ,"ERROR", JOptionPane.ERROR_MESSAGE);
		}
		if(flag == 1)
		{
			JOptionPane.showMessageDialog(null," Question Deleted Successfully " ,"Question Deleted", JOptionPane.PLAIN_MESSAGE);
			
		}
		if(flag == 2)
		{
			JOptionPane.showMessageDialog(null," Question ID does not exist " ,"Question was not Deleted", JOptionPane.ERROR_MESSAGE);
		}
		
	}
	
	private void btnCancelActionPerformed(java.awt.event.ActionEvent evt)
	{
		QuesSetUp qs = new QuesSetUp();
		qs.frame.setVisible(true);
		this.frame.setVisible(false);
	}
}
